/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.restclient.services;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.restclient.App;
import cz.muni.fi.pa165.machineryrental.restclient.utils.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author David Katuscak, 359273
 */
public class CustomerRestServiceImpl implements CustomerRestService {

    private final static String URL_GET_BY_ID = "customerRest/getById/";
    private final static String URL_FIND_ALL = "customerRest/findAll";
    private final static String URL_CREATE = "customerRest/create";
    private final static String URL_UPDATE = "customerRest/update/";
    private final static String URL_REMOVE = "customerRest/remove/";
    private final static String URL_FIND_BY_COMPANY_NUMBER = "customerRest/findByCompanyNumber/";
    private final static String URL_FIND_BY_EMAIL = "customerRest/findByEmail/";
    private final static String URL_FIND_BY_LAST_NAME = "customerRest/findByLastName/";
    private final static String URL_IS_EMAIL_UNIQUE = "customerRest/isEmailUnique/";
    private final static String URL_IS_VAT_UNIQUE = "customerRest/isVATUnique/";
    private final static String URL_IS_COMPANY_NUMBER_UNIQUE = "customerRest/isCompanyNumberUnique/";
        
    @Override
    public CustomerDTO getById(long id) {

        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_GET_BY_ID + id;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);


        ResponseEntity<CustomerDTO> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                CustomerDTO.class);

        return response.getBody();
    }

    @Override
    public List<CustomerDTO> findAll() {
        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_FIND_ALL;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<CustomerDTO[]> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                CustomerDTO[].class);

        List<CustomerDTO> customers = Arrays.asList(response.getBody());
        return customers;
    }

    public void create(CustomerDTO customer) {
        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_CREATE;
        String username = App.getUsername();
        String password = App.getPassword();

        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.POST,
                new HttpEntity<Object>(customer, Utils.createJSONHeaders(username, password)),
                Void.class);
    }

    public void update(CustomerDTO customer) {
        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_UPDATE + customer.getId();
        String username = App.getUsername();
        String password = App.getPassword();

        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.PUT,
                new HttpEntity<Object>(customer, Utils.createJSONHeaders(username, password)),
                Void.class);
    }

    public void remove(long id) {
        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_REMOVE + id;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.DELETE,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                Void.class);
    }

    public List<CustomerDTO> findByLastName(String lastName) {

        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_FIND_BY_LAST_NAME + lastName;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<CustomerDTO[]> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                CustomerDTO[].class);

        List<CustomerDTO> customers = Arrays.asList(response.getBody());
        return customers;
    }

    public CustomerDTO findByEmail(String email) {
        RestTemplate restTemplate = new RestTemplate();
        
        String url = App.getUrl() + URL_FIND_BY_EMAIL + email + "/";
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<CustomerDTO> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createHeaders(username, password)),
                CustomerDTO.class);

        return response.getBody();
    }

    public CustomerDTO findByCompanyNumber(String companyNumber) {
        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + URL_FIND_BY_COMPANY_NUMBER + companyNumber;
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<CustomerDTO> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                CustomerDTO.class);

        return response.getBody();
    }

    public boolean isEmailUnique(String email) {
        RestTemplate restTemplate = new RestTemplate();
        
        String url = App.getUrl() + URL_IS_EMAIL_UNIQUE + email + "/";
        
        System.out.println("Uniquenes of email is going to be checked: " + url);
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<Boolean> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createHeaders(username, password)),
                Boolean.class);

        return response.getBody();
    }

    public boolean isVATUnique(String vat) {
        RestTemplate restTemplate = new RestTemplate();
        
        String url = App.getUrl() + URL_IS_VAT_UNIQUE + vat;
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<Boolean> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createHeaders(username, password)),
                Boolean.class);

        return response.getBody();
    }

    public boolean isCompanyNumberUnique(String companyNumber) {
        RestTemplate restTemplate = new RestTemplate();
        
        String url = App.getUrl() + URL_IS_COMPANY_NUMBER_UNIQUE + companyNumber;
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<Boolean> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createHeaders(username, password)),
                Boolean.class);

        return response.getBody();
    }
}
